require("tsp")




-- input("bilinwl168@163.com")
-- input("zkx959955..")




t = {}
degree = 90
t['背包红']={0xed0a00, "0|0|0xed0a00,2|13|0xed0a00",degree,1198,35,1229,72}
    t['背包白']={0xede5db, "0|0|0xede5db,0|18|0xede5db",degree,1183,20,1241,82}
t['出售按钮']={0x722424, "0|0|0x722424,-29|-26|0xd14946,57|26|0xbc3636,109|-1|0x292928",degree,924,635,1230,709}
t['自动添加']={0xddd5cb, "0|0|0xddd5cb,3|0|0x282725,140|1|0x6c2323,402|-16|0x712626",degree,753,644,1224,719}
t['自动添加2']={0x312f2d, "0|0|0x312f2d,-7|-5|0xebe3d9,-165|-246|0xcc5993,-165|-166|0xe69133",degree,481,319,797,665}
t['自动添加2']={0x32302e, "0|0|0x32302e,4|-5|0xe6ded6",degree,618,620,716,643}
t['出售']={0xe4d367, "0|0|0xe4d367,245|-14|0x712626,251|25|0x682121,-76|4|0xede5db",degree,755,646,1224,718}
    t['背包英雄出售']={0xebe3d9, "0|0|0xebe3d9,71|-26|0x3a3937,212|-361|0xede5db",degree,523,164,968,565}


function sell()
    -- body
    if d("背包白",true) or d("背包红",true)  then  end
    
    d("出售按钮",true)
    d("自动添加",true)
    d("自动添加2",true)
    if d("出售",true) then
        d("背包英雄出售",true)
        return true
    end
    d("背包英雄出售",true)
end

function showLog(txt)
	adbox = adbox or 0
	if adbox == 0 then
		adbox = 1
		--创建文本视图
		fwShowWnd("window1",95,8,270,34,1);
		mSleep(500)
	end
	fwShowTextView("window1","text1",txt,"center","FF0000","FFDAB9",10,0,0,0,170,26,0.8);
	--fwShowTextView("window1","text1","文本视图","center","FF0000","FFDAB9",10,0,0,0,200,30,0.5);
end

-- function log(txt)
--     showLog(txt)
-- end

t['弹窗-广告x']={0xffffff, "0|0|0xffffff,1|8|0x000000,5|30|0xffcc00,-30|-3|0xffcc00",degree,1182,12,1265,89}
t['弹窗-广告x']={0xffffff, "0|0|0xffffff,0|-11|0x000000,-7|-7|0xffffff,8|8|0xffffff,8|1|0x000000",degree,852,78,1166,316}
t['弹窗-获得目录']={0xede5db, "0|0|0xede5db,0|-5|0x181716,-229|117|0xb59a51",degree,852,78,1166,316}
t['弹窗-最右上角广告x']={0xffffff, "0|0|0xffffff,-1|-25|0xfafafa,-1|-19|0x000000,-1|24|0xfbfbfb,0|11|0x000000",degree,1192,12,1262,75}
t['弹窗-死亡广告x']={0xefe5db, "0|0|0xefe5db,0|-8|0x181716,7|-6|0xefe5db,9|0|0x181716,7|5|0xefe5db",degree,1092,77,1170,145}
t['弹窗-复活卡住']={0x32302e, "0|0|0x32302e,513|-370|0x050608,120|-15|0x712626,386|23|0x682121",degree,317,123,1006,620}

init(1)
game = {}
game.bid = "com.netmarble.survivalgb"
game.changeKey = true
game.move = true
game.auto = false
game.live = 0
game.changeLine = false
game.date = os.date("%x", os.time())
game.agin = true

t['地图-特殊冒险']={0xede5db, "0|0|0xede5db,23|6|0x8bc6df,-86|1|0xede5db,33|-381|0xede5db",degree,1087,21,1303,678}
t['地图-特殊冒险-进入']={0xebe1d7, "0|0|0xebe1d7,146|-24|0xc0403d,-133|-25|0xc13b3a",degree,1001,642,1325,727}
    t['地图-特殊冒险-选择进入']={0xede5db, "0|0|0xede5db,-97|-24|0xc23b3b,98|-24|0xc63d3c",degree,934,648,1179,724}
    t['地图-特殊冒险-选择进入']={0xefe5db, "0|0|0xefe5db,2|-17|0x8e2e2c",degree,912,591,1196,708}
    地图选择 = {{616, 654, 0x67615c},{402, 657, 0xc3bab2},{167, 659, 0x968c84}}
    t['地图-已经在地图']={0xf14135, "0|0|0xf14135,0|3|0xee4033",degree,526,70,789,283}
    
t['地图-怪-展开']={0xfdfdfb, "0|0|0xfdfdfb,0|-5|0x30353a,7|-7|0xf5f5f3,-33|-10|0xfefefe,-38|12|0x34363b",degree,1211,104,1321,721}
    t['地图-怪-蓝']={0x252f65, "0|0|0x252f65,6|0|0xede3db,3|-10|0x252e65,-6|5|0x252f65",degree,1017,99,1143,739}
    t['地图-怪-红']={0x5e1f1e, "0|0|0x5e1f1e,6|-1|0xede5d7,7|10|0x5f201f,-9|2|0x5e201e",degree,1017,99,1143,739}
    t['地图-怪-绿']={0xebe5db, "0|0|0xebe5db,8|-6|0x1f582c,-7|-7|0x1f582c,-8|7|0x1f582c",degree,950,391,1308,612}
    t['地图-怪-底部']={0x161414, "0|0|0x161414,31|-9|0xede3db,50|-6|0xece2da,4|-9|0x342f2f",degree,932,330,1042,735}
t['地图-移动']={0xe4dcd3, "0|0|0xe4dcd3,19|5|0xd9cdc3,-5|39|0xe7dbd1,-6|-26|0xe2bcb0",75,100,40,884,620}
t['地图-还在移动中']={0xc5ffff, "0|0|0xc5ffff,0|-1|0xb6ffff",degree,183,119,784,597}
t['地图-X']={0xede5db, "0|0|0xede5db,7|-1|0x000822,6|-7|0xede5db",degree,1264,29,1297,73}
t['地图-怪-收起']={0xede3db, "0|0|0xede3db,299|-22|0xfef2c2,391|-11|0xe7d558",degree,7,628,461,706}

function changeMaps()
    log('换一下地图')
    if ( game.changeKey ) then
        click( 1280,54,2 )
        d("地图-特殊冒险",true,1,2)
        d("地图-特殊冒险-进入",true,1,2)
        
        地图选择包 = {}
        地图选择包['1阶地图'] = 1
        地图选择包['2阶地图'] = 2
        地图选择包['3阶地图'] = 3
        
        click( 地图选择[地图选择包[选阶]][1],地图选择[地图选择包[选阶]][2],2 )
        d("地图-特殊冒险-选择进入",true,1,0.5)
        if ( d("地图-已经在地图") ) then
            game.changeKey = false
        end
    elseif ( game.move ) then
        click(1176,104,3)
        d("地图-怪-展开",true,1,2)
        d("地图-怪-收起",true,1,2)
        
        down = rd(2,3)
        for i=1,down do
            moveTo(1115,570,1109,243,5)
            delay(1)
            if d("地图-怪-底部") then
                up = rd(1,3)
                for i = 1 , up do
                    moveTo(1115,243,1109,570,5)
                    delay(1)
                end
                break
            end
        end
        
        delay(1)
        -- 怪 =  d("地图-怪-蓝",true,1,2) or d("地图-怪-红",true,1,2) or d("地图-怪-绿",true,1,2)
        click(1144,120+73*rd(0,7),2)
        
        if ( d("地图-移动",true,1,2) )then
            local i = 0
            while i < 30 and d('地图-还在移动中') do
                i = i + 1
                delay(1)
            end
            game.move = false
            game.auto = true
        end
    end
end



t['游戏-魂兽-复活']={0x744905, "0|0|0x744905,31|34|0x010000,-31|-26|0x030303,28|-27|0x020202,-28|32|0x020102",degree,1111,304,1180,375}
t['游戏-魂兽-金币复活']={0x712626, "0|0|0x712626,-132|-4|0xe7d676,-227|-366|0xeee4db,-378|-383|0x2f2d2a,-80|-182|0xefb102",degree,357,155,915,585}
t['游戏-魂兽-金币复活-免费']={0xede3da, "0|0|0xede3da,121|1|0xefda65,310|8|0x692121,405|-14|0x712626,453|-353|0xefe5db",degree,354,153,970,587}

t['游戏-魂兽-魂兽出战']={0x7b7571, "0|0|0x7b7571,19|-3|0x020202,23|-7|0x7b7771,-21|25|0x7a7571,-29|35|0x010102",degree,1106,296,1185,378}


function soul()
    log('监控魂兽')
    if d("游戏-魂兽-复活",true,1,2)then
        click(945,242)
        d("游戏-魂兽-金币复活-免费",true)
    elseif d("游戏-魂兽-魂兽出战",true)then
        click(945,242)
    else
        click(1264,338)
    end
end


t['换线-界面']={0x037f21, "0|0|0x037f21,108|-2|0x003ea3,-4|18|0x8900a7",degree,190,595,465,675}
    t['换线-左上角定位']={0x212127, "0|0|0x212127,5|4|0x0c0b0a,73|4|0x0c0b0a,102|-40|0x69696a",degree,211,189,342,363}
    t['']={0x0c0b0a, "0|0|0x0c0b0a,7|-64|0x212127,55|-11|0x212127",degree,190,595,465,675}

function changeLine()
    if game.changeLine then
        click(1274,107,2)
        local i = 0
        while i < 20 do
            log('换线界面')
            if d("换线-左上角定位") then
                local o = {x+7,y-64,x+55,y-10}
                ocrNumber = ocrText(o[1],o[2],o[3],o[4],0)
                if ocrNumber then
                    log(ocrNumber)
                    ocrNumber = tonumber(ocrNumber)
                    log( {线路线择,ocrNumber} )
                    if 线路线择 - ocrNumber < 3 then
                        click(252,243+(线路线择-ocrNumber)*88 )
                        game.changeLine = false
                    else
                        moveTo(465,537,458,260,2)
                        delay(1)
                    end
                end
            end
            i = i+1
        end
    end
end


t['弹窗-网络断点']={0xfae6b3, "0|0|0xfae6b3,4|0|0x32302e,93|-229|0xeadcce,319|-315|0xefe5db,-287|-315|0xefe5db",degree,302,164,1005,546}

t['事件-开始游戏']={0xede5db, "0|0|0xede5db,125|-34|0xc53c3b,-168|31|0xbc3636,-1113|-630|0xe3d9cb",degree,20,14,1317,727}
t['事件-据点复活']={0xede5db, "0|0|0xede5db,115|2|0x712626,374|1|0x712626",80,468,528,905,559}
t['事件-据点复活']={0x712626, "0|0|0x712626,244|41|0x682121,246|1|0x712626,357|-163|0x79624f,170|-175|0x816855",degree,452,321,988,588}
t['事件-返回']={0xe5d9cb, "0|0|0xe5d9cb,22|1|0x3d3532,36|-3|0x695f5b",degree,11,12,78,70}
t['事件-返回6s']={0xe5dbcf, "0|0|0xe5dbcf,22|2|0x353030,22|-19|0x685f5a,21|21|0x675f59",degree,3,10,75,70}
t['事件-省电模式']={0xffffff, "0|0|0xffffff,-24|6|0xffffff,-7|147|0xf9a8af,-5|147|0xf39696",degree,600,296,744,519}
t['事件-省电中']={0xf29799, "0|0|0xf29799,-5|0|0xcb5949,9|0|0xaf4737",degree,603,469,732,482}

t['游戏-主界面']={0xfd0b00, "0|0|0xfd0b00,-1|0|0xfd0b00",degree,19,56,39,81}
    t['游戏-主界面-手动']={0xede5db, "0|0|0xede5db,17|0|0xe5ddd3,29|-1|0xeae2d7",degree,416,691,469,722}
    t['游戏-主界面-自动']={0xefbf4f, "0|0|0xefbf4f,0|-1|0xefbf50",degree,397,667,487,745}
    
    t['游戏-战斗庇护不足']={0xefe5db, "0|0|0xefe5db,-4|-2|0x080a0a,4|-2|0x0b0e0e",degree,637,510,656,525}
    t['游戏-矿石未满']={0xef9509, "0|0|0xef9509,0|-3|0xef9509,5|-5|0xeb9209,14|-5|0xe68f0a",degree,582,524,713,542}
    t['游戏-退出副本']={0xefe5db, "0|0|0xefe5db,15|-4|0xe8e0d5,15|5|0xebe2d8,27|-19|0xefe7db,27|20|0xefe7db",degree,912,22,964,78}



技能监控 = {{1053, 665, 0xd24008},{1071, 666, 0xffffff},{1032, 530, 0xf9f9f7},{1042, 531, 0xf9fafa},
    {1139, 455, 0xfffcfc},{1129, 445, 0xfde9e3},{1255, 466, 0xe7c992},{1264, 466, 0xc4801c}}





-- require "TSLib"
w,h = getScreenSize()
UINew(1,"第1页","A3开始","取消","a3",1,0,h,w,"255,255,255","234,241,222","","default",1,15,"left")
UILabel(1,"特殊冒险",15,"left","38,38,38")
UISwitch(1,"特殊冒险","off","m","left")
UIRadio(1,"选阶","1阶地图,2阶地图,3阶地图","1")
UILabel(1,"清理背包",15,"left","38,38,38")
UISwitch(1,"清理背包","on","m","left")
UILabel(1,"魂兽出战",15,"left","38,38,38")
UISwitch(1,"魂兽出战","on","m","left")
UILabel(1,"线路选择",15,"left","38,38,38")
UICombo(1,"线路线择","1,2,3,4,5,6,7,8,9,10,11","0")
ret=UIShow(0)
if ret == 1 then
    log("开始运行")
    log( {特殊冒险,选阶,清理背包,线路线择})
    线路线择 = tonumber(线路线择) + 1
elseif  ret == 0 then
    log("取消运行")
    lua_exit()
end





TimeLine = 0
tips_times = 0
restTimes = 0
技能状态 = {}
技能保持次数 = 0

die_times = 0
while (true) do
    
    if os.date("%x", os.time()) ~= game.date then
        game.changeKey = true
        game.move = true
        game.auto = false
        game.live = 0
        game.changeLine = false
        game.date = os.date("%x", os.time())
        game.agin = true
    end
    
    if game.agin and os.date("%H", os.time()) == 13 then
        game.changeKey = true
        game.move = true
        game.auto = false
        game.agin = false
    end
    
    
    if active(game.bid ,8) then
        if ( d("游戏-主界面") ) then
            tips_times = 0
            
            if game.auto then
                if (d("游戏-主界面-自动") ) then
                    
                    --这是城监控魂兽的地方
                    if 魂兽出战 == "on" then
                        soul()
                    end
                    
                    if d("游戏-矿石未满") then
                        game.live = 0
                    elseif (d("游戏-战斗庇护不足")) then
                        showLog("庇护不足")
                    else
                        game.live = game.live + 1
                        if ( game.live > 30 ) then
                            -- d("游戏-退出副本", true)
                            -- dialog("满10w退出副本",5)
                            showLog("可能矿满")
                        end
                    end
                    
                    
                    keepScreen(true)
                    index = 0
                    for k,v in ipairs(技能监控) do
                        color = getColor(v[1], v[2])
                        color = string.format("0x%X",color)
                        if 技能状态[k] then
                            if isColor(技能状态[k][1],技能状态[k][2],技能状态[k][3],85) then
                                index = index + 1
                            else
                                技能状态[k] = {v[1],v[2],color}
                            end
                        else
                            技能状态[k] = {v[1],v[2],color}
                        end
                    end
                    keepScreen(false)
                    
                    if index >= 8 then
                        技能保持次数 = 技能保持次数 + 1
                        if 技能保持次数 > 10 then
                            showLog('卡住了,切换地图')
                            game.changeKey = true
                            game.auto = false
                            game.move = true
                        end
                    else
                        技能保持次数 = 0
                    end
                    
                    log( {index,技能保持次数} )
                    
                elseif d("游戏-主界面-手动",true) then
                else
                    click(446,710)
                end
                
            elseif 特殊冒险 == "on" then
                changeMaps()
            else
                game.auto = true
            end
            
            if 清理背包 == "on" then
                if (  os.time() - TimeLine > 2*60 ) then
                    if sell() then
                        TimeLine = os.time()
                    end
                end
                showLog( "清理倒计时" .. 120 - (os.time() - TimeLine)  )
            end
        else
            log("事件处理")
            
            if d("事件-据点复活") then
                click(523,545,2)
                game.auto = false
                game.changeKey = true
                game.move = true
                die_times = die_times + 1
            else
                d("事件-开始游戏",true,1,2)
                if d("事件-省电模式") or d("事件-省电中")then
                    moveTo(500,626,1200,626,20,10)
                end
                
                tips_times = tips_times + 1
                if tips_times %8 == 0 then
                    log("点击一次屏中间")
                    click(40,40,2)
                elseif tips_times %15 == 0 then
                    click(300,500,2)
                elseif tips_times %100 == 0 then
                    closeApp(game.bid,1)
                    delay(1)
                end
                d("事件-返回",true)
                d("事件-返回6s",true)
                d("地图-X",true)
    
                d("弹窗-获得目录",true)
                d("弹窗-广告x",true)
                d("弹窗-最右上角广告x",true)
                d("弹窗-死亡广告x",true)
                d("弹窗-复活卡住",true)
                d("弹窗-网络断点",true)
            end
            
        end
    else
        game.auto = false
        game.changeKey = true
        game.move = true
    end
    delay(1)
end










